<?php
//包含bootstrap的顶端按钮
include '../PHP/head.php';
?>

<script type="text/javascript">
//需要一个异步删除记录的函数

	$(document).ready(function() {
    $('button.deletedata').on('click', function() {
					var requestData = {code: $(this).attr("id"), key: "id", table: "users"};
                    $('#notes').load('del_data.php', requestData);
                    $(this).parent().parent().hide();
					});

			});
</script>



<br />

<p class="text-center lead">修改用户表</p>
<br />
<form class="form-inline" action = "../PHP/add_user_form.php" method = "post">
<div class="form-group">
<label for="user_id">姓名拼音：</label>
<input class="form-control" placeholder="请输入姓名拼音" type = "text" name = "user_id" />
</div>
<div class="form-group">
<label for="user_name">姓名：</label>
<input class="form-control" placeholder="请输入姓名" type = "text" name = "user_name" />
</div>
<div class="form-group">
	<label for="position">职务：</label>
	<select class="form-control" name='position'>
		<option value='EI'>电仪专业工程师</option>
		<option value='ST'>结构专业工程师</option>
		<option value='ME'>机械专业工程师</option>
		<option value='VE'>静设备专业工程师</option>
		<option value='PI'>管道专业工程师</option>
		<option value='SA'>销售员</option>
		<option value='LEI'>电仪专业负责人</option>
		<option value='LST'>结构专业负责人</option>
		<option value='LME'>机械专业负责人</option>
		<option value='LVE'>静设备专业负责人</option>
		<option value='LPI'>管道专业负责人</option>
		<option value='RP'>报价负责人</option>
		<option value='DC'>文控</option>
		<option value='MSA'>销售经理</option>
	</select>
</div>
<div class="form-group">
<label for="pass_wd">密码：</label>
<input class="form-control" placeholder="请输入密码" type = "password" name = "pass_wd" />
</div>
<div class="form-group">
<label for="pass_wd_re">再次输入密码：</label>
<input class="form-control" placeholder="请再次输入密码" type = "password" name = "pass_wd_re" />
</div>
<button type="submit" class="btn btn-default">增加</button>
</form>
<div id="notes" class="insert">
<p>操作信息栏</p>
</div>
<?php
//引用必要的函数
require_once ('../FNS/db_fns.php');

if(isset($_POST['user_id'])) {
    
//创建变量并清除前后空格
$user_id = trim ($_POST['user_id']);
$pass_wd = trim ($_POST['pass_wd']);
$pass_wd_re = trim ($_POST['pass_wd_re']);
$user_name = trim ($_POST['user_name']);
$position = trim ($_POST['position']);


//判断是否为空
if (!$pass_wd || !$user_id || !$pass_wd_re || !$user_name || !$position){

echo "所有输入不能为空，请返回重新输入！";
}


//判断两次密码输入一致
if ($pass_wd <> $pass_wd_re) {
echo "两次密码输入不一致，请返回！";
exit;
}


//数据过滤和处理
//加引号避免输入数据库出错

if (! get_magic_quotes_gpc()) {
$pass_wd=addslashes ($pass_wd);
$user_id=addslashes ($user_id);
$user_name=addslashes ($user_name);
$position=addslashes ($position);
}

//连接数据库并搜索数据

$db= db_connect();

$query="insert into users values
  ('".$user_id."','".$user_name."','".$pass_wd."','".$position."')";
$result=$db->query ($query);
if ($result){
echo "用户增加成功。";
}else{
echo"出错，用户没有加入。";
}


$db->close();
}

?>


<br />
<br />
<p class="text-center lead">用户列表：</p>

<?php
//引用必要的函数
require_once ('../FNS/db_fns.php');


//连接数据库并搜索数据

$db= db_connect();

$query="select * from users";
$result=$db->query ($query);

//获取记录数量
$num_results=$result->num_rows;


//写表头
echo "<table class='table table-striped'>";
echo "<tr>";
echo "<td align='center'><strong>用户名</strong></td>";
echo "<td align='center'><strong>姓名</strong></td>";
echo "<td align='center'><strong>职务代码</strong></td>";
echo "<td align='center'>操作</td>";
echo "</tr>";



//循环编制表格
for ($i=0; $i < $num_results; $i++) {

	$row=$result->fetch_assoc ();
	
	$user_id = stripslashes ($row['id']);
	$user_name = stripslashes ($row['name']);
	$user_position = stripslashes ($row['position']);
	
	echo "<tr>";
	echo "<td align ='center'>".$user_id."</td>";
    echo "<td align ='center'>".$user_name."</td>";
	echo "<td align ='center'>".$user_position."</td>";
	echo "<td align='center'><button class='btn btn-default deletedata' id='".$user_id."'>删除</button></td>";
	echo "</tr>";
	
}
echo "</table>";


$result->free();
$db->close();

?>



</div>
</body>
</html>
